// src/workers/fetchFollowStatus.worker.js
import { followFriendUserAPI } from '@/api/search';

self.onmessage = async (event) => {
  const { currentUserId, targetUserIds } = event.data;

  // 去重，避免重复请求同一个用户
  const uniqueUserIds = [...new Set(targetUserIds)];

  // 并发请求所有关注状态
  const promises = uniqueUserIds.map(async (targetUserId) => {
    try {
      const res = await followFriendUserAPI(currentUserId, targetUserId);
      const isFollowing = res.data === '关注成功'; // 根据你的 API 返回逻辑
      return { targetUserId, isFollowing };
    } catch (error) {
      console.error(`获取用户 ${targetUserId} 关注状态失败:`, error);
      return { targetUserId, isFollowing: false };
    }
  });

  const results = await Promise.all(promises);

  // 构建 { userId: isFollowing } 的映射对象
  const followStatusMap = {};
  results.forEach(({ targetUserId, isFollowing }) => {
    followStatusMap[targetUserId] = isFollowing;
  });

  self.postMessage(followStatusMap);
};